//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmHestonSolver.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Experimental/Finitedifferences/FdmStepConditionComposite.h>
#include <gen/QL/Experimental/Finitedifferences/FdmInnerValueCalculator.h>
#include <gen/QL/Experimental/Finitedifferences/FdmQuantoHelper.h>
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (boost::shared_ptr<QuantLib::FdmHestonSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonSolver = NULL;
#endif
	_ppFdmHestonSolver = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (QuantLib::FdmHestonSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmHestonSolver = NULL;
#endif
	_ppFdmHestonSolver = new boost::shared_ptr<QuantLib::FdmHestonSolver> (&childNative);
    
    _FdmHestonSolverOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (CFdmHestonSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonSolver = new boost::shared_ptr<QuantLib::FdmHestonSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonSolver::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (QuantLib::Handle<QuantLib::FdmHestonSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonSolver = &childNative;
	_ppFdmHestonSolver = &static_cast<boost::shared_ptr<QuantLib::FdmHestonSolver>>(childNative.currentLink());
    
    _FdmHestonSolverOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (QuantLib::Handle<QuantLib::FdmHestonSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonSolver = &childNative;
	_ppFdmHestonSolver = &static_cast<boost::shared_ptr<QuantLib::FdmHestonSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::CFdmHestonSolver (QuantLib::FdmHestonSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonSolver = NULL;
#endif
	_ppFdmHestonSolver = new boost::shared_ptr<QuantLib::FdmHestonSolver> (new QuantLib::FdmHestonSolver (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::~CFdmHestonSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmHestonSolver != NULL)
    {
	    delete _ppFdmHestonSolver;
        _ppFdmHestonSolver = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::!CFdmHestonSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmHestonSolver != NULL)
    {
	    delete _ppFdmHestonSolver;
    }
}
QuantLib::FdmHestonSolver& Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::GetReference ()
{
    if (_ppFdmHestonSolver == NULL) throw gcnew NativeNullException ();
	return **_ppFdmHestonSolver;
}
boost::shared_ptr<QuantLib::FdmHestonSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::GetShared ()
{
    if (_ppFdmHestonSolver == NULL) throw gcnew NativeNullException ();
	return *_ppFdmHestonSolver;
}
QuantLib::FdmHestonSolver* Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::GetPointer ()
{
    if (_ppFdmHestonSolver == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmHestonSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::GetHandle ()
{
	if (_phFdmHestonSolver == NULL)
	{
		_phFdmHestonSolver = new Handle<QuantLib::FdmHestonSolver> (*_ppFdmHestonSolver);
	}
	return *_phFdmHestonSolver;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::HasNative () 
{
	return (_ppFdmHestonSolver != NULL);
}

Double Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::ThetaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonSolver)->thetaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::ValueAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonSolver)->valueAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::DeltaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonSolver)->deltaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::GammaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonSolver)->gammaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::MeanVarianceDeltaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonSolver)->meanVarianceDeltaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmHestonSolver::MeanVarianceGammaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonSolver)->meanVarianceGammaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

